﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.User
{
    public class PostPm : UserBasePage
    {
        protected MessageInfo Msg = new MessageInfo();
        protected string receivers;

        protected List<UserInfo> FriendList;
        protected int FriendCount;

        protected override void ShowPage()
        {

            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (RequestTools.IsPost()) HandlePost();
            else HandleGet();

        }

        private void HandleCommon()
        {
            this.AddLocation("写新消息", "/User/PostPm.aspx");
            this.SetTitle("写新消息");
            FriendList = FriendBLL.Instance.GetFriendList(10000000, 1, UserData.Id, Constants.FriendPass, out FriendCount);
        }

        private void HandlePost()
        {
            if (!CheckPermissionAfterPost())
            {
                return;
            }

                string[] array = receivers.Split(';');
                bool succ = true;
                int i;
                for (i = 0; i < array.Length; i++)
                {

                    UserInfo info = UserBLL.Instance.GetUserInfo(array[i]);
                    if (info.Id <= 0)
                    {
                        this.AddErrLine("用户" + array[i] + "不存在！请确认您的输入无误");
                        return;
                    }

                    Msg.msgto = info.UserName;
                    Msg.msgtoid = info.Id;
                    Msg.msgfrom = UserData.UserName;
                    Msg.msgfromid = UserData.Id;
                    Msg.PostDateTime = DateTime.Now;
                    Msg.message = RequestTools.GetString("message").Trim();
                    Msg.ParentId = 0;
                    Msg.IsRead = 0;

                    succ = MessageBLL.Instance.CreateMessage(Msg);
                    if (!succ) break;

                }
                if (succ)
                {
                    this.AddMsgLine("已成功发送！");
                    this.SetUrl("/User/PostPm.aspx");
                    this.SetMetaRefresh();
                }
                else
                {
                    this.AddErrLine("给用户" + array[i] + "发送互动失败！");
                    return;

                }




        }

        private void HandleGet()
        {


        }


        #region 发送后提交后检查
        private bool CheckPermissionAfterPost()
        {

            receivers = RequestTools.GetFormString("receivers");
            if (receivers == "")
            {
                AddErrLine("接收人不能为空");
                return false;
            }


            if (RequestTools.GetString("message").Equals(""))
            {
                this.AddErrLine("内容不能为空");

                return false;
            }

            if (RequestTools.GetString("message").Length > 3000)
            {
                AddErrLine("内容不能超过3000字");

                return false;
            }

            return true;
        }
        #endregion
    }


}
